<template>
  <div class="global_company_info main_white marginTop10">
    <div class="main_white padding10 paddingTop0">
      <div class="main_ti">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="基本信息" name="0"></el-tab-pane>
          <el-tab-pane label="公司资质" name="1"></el-tab-pane>
        </el-tabs>

        <div class="mainua_form">
          <el-form
            :inline="false"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="140px"
            v-show="activeName == 0"
          >
            <el-form-item label="公司LOGO：" prop="pic">
              <div class="main_upImg marginTop5">
                <el-upload
                  :headers="{
                    Authorization:getToken()
                  }"
                  :action="uploadPath"
                  list-type="picture-card"
                  :on-success="handSuccess"
                  :show-file-list="false"
                  :data="uploadparams"
                  :before-upload="beforeUpload"
                >
                  <span slot="default">
                    <svg
                      aria-hidden="true"
                      class="main_page_svg"
                      width="32"
                      height="27"
                    >
                      <use xlink:href="#icon-commodit_camera" />
                    </svg>
                  </span>
                </el-upload>
                <img v-if="form.pic" :src="form.pic" class="main_avatar" />
                <div class="clearfix"></div>
              </div>
              <div class="global_cu">建议尺寸（220*80）</div>
            </el-form-item>
            <el-form-item label="公司公章：" prop="pic">
              <div class="main_upImg marginTop5">
                <el-upload
                  :headers="{
                    Authorization:getToken()
                  }"
                  :action="uploadPath"
                  list-type="picture-card"
                  :on-success="handSuccess_offical_seal"
                  :show-file-list="false"
                  :before-upload="beforeUpload"
                >
                  <span slot="default">
                    <svg
                      aria-hidden="true"
                      class="main_page_svg"
                      width="32"
                      height="27"
                    >
                      <use xlink:href="#icon-commodit_camera" />
                    </svg>
                  </span>
                </el-upload>
                <img v-if="form.offical_seal" :src="form.offical_seal" class="main_avatar" />
                <div class="clearfix"></div>
              </div>
              <div class="global_cu">建议尺寸（600*600）</div>
            </el-form-item>
            <el-form-item label="公司名称：" prop="company_fullname">
              <el-input
                disabled
                v-model="form.company_fullname"
                placeholder="请输入平台名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="公司简称：" prop="site_title">
              <el-input
                v-model="form.site_title"
                placeholder="请输入平台名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="客服电话：" prop="site_phone">
              <el-input
                v-model="form.site_phone"
                placeholder="请输入客服电话"
              ></el-input>
            </el-form-item>

            <!-- <el-form-item label="微信客服：" prop="wx_customer_service">
              <el-input
                v-model="form.wx_customer_service"
                placeholder="请输入微信客服链接"
              ></el-input>
            </el-form-item> -->

            <el-form-item prop="wx_customer_service">
              <span slot="label">
                <el-tooltip
                  class="item marginRight5 mainor_tei"
                  effect="dark"
                  placement="bottom"
                >
                  <div slot="content">
                    填入企业微信客服链接后，客户将可选择联系电话或在线客服进行咨询
                  </div>
                  <i class="el-icon-question"></i> </el-tooltip
                >微信客服：
              </span>
              <el-input
                v-model="form.wx_customer_service"
                placeholder="请输入微信客服连接"
              ></el-input>
            </el-form-item>

            <el-form-item prop="wx_customer_service">
              <span slot="label">
                <el-tooltip
                  class="item marginRight5 mainor_tei"
                  effect="dark"
                  placement="bottom"
                >
                  <div slot="content">
                    登录企业微信管理后台，在企业信息处复制企业ID
                  </div>
                  <i class="el-icon-question"></i> </el-tooltip
                >企业ID：
              </span>
              <el-input
                v-model="form.corpId"
                placeholder="请输入企业ID"
              ></el-input>
            </el-form-item>

            <el-form-item label="公司地址：" prop="company_address">
              <el-input
                v-model="form.company_address"
                placeholder="请输入公司地址"
              ></el-input>
            </el-form-item>

            <el-form-item prop="receive_man">
              <span slot="label">
                <el-tooltip
                  class="item marginRight5 mainor_tei"
                  effect="dark"
                  placement="bottom"
                >
                  <div slot="content">在供应链批发业务中使用</div>
                  <i class="el-icon-question"></i> </el-tooltip
                >收货人：
              </span>
              <el-input
                v-model="form.receive_man"
                placeholder="请输入收货人"
              ></el-input>
            </el-form-item>

            <el-form-item label="收货电话：" prop="receive_mobile">
              <el-input
                v-model="form.receive_mobile"
                placeholder="请输入收货电话"
              ></el-input>
            </el-form-item>
            <el-form-item label="收货地址：" prop="receive_address">
              <el-input
                v-model="form.receive_address"
                placeholder="请输入收货地址"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item prop="site_notice">
          <span slot="label">
            <el-tooltip
              class="item marginRight5 mainor_tei"
              effect="dark"
              placement="bottom"
            >
              <div slot="content">展示的地方为移动端首页海报下方！</div>
              <i class="el-icon-question"></i> </el-tooltip
            >公告：
          </span>
          <el-input
            type="textarea"
            v-model="form.site_notice"
            placeholder="请输入公告内容"
          ></el-input>
        </el-form-item> -->
          </el-form>
          <div v-show="activeName == 1">
            <div class="maincla_ed">
              <Editor
                api-key="vl7byj8kh7hyvvi6o7137s7khcpfsp762987dlnbcd93u37a"
                :init="init"
                v-model="form.company_qualification"
              ></Editor>
            </div>
          </div>
          <div class="clearfix"></div>
          <div class="newConBox_p100 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="[
                  'mainua_an newConBox ',
                  { on: searchBarFixed },
                  { marginLeft5: !searchBarFixed }
                ]"
              >
                <el-button type="primary" @click="preservation" v-repeatClick
                  >保存</el-button
                >
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { uploadPath } from "@/api/currency";
import { setting_info } from "@/api/global/globalSystem";
import { setting_company } from "@/api/currency";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import Editor from "@tinymce/tinymce-vue";
import init from "@/views/commodity/init";

export default {
  components: {
    Editor
  },
  computed: {
    ...mapGetters(["avatar"])
  },
  data() {
    return {
      // 富文本
      init,
      // 上传图片
      uploadPath,
      // 搜索
      form: {
        site_logo: "",
        pic: "",
        // 公章上传路径
        offical_seal:"",
        site_title: "",
        site_phone: "",
        company_address: "",
        receive_man: "",
        receive_mobile: "",
        receive_address: "",
        site_notice: "",
        company_qualification: "",
        wx_customer_service: "",
        corpId: ""
      },
      // 监听滚动
      searchBarFixed: false,
      // 上传图片参数
      uploadparams: {
        store: "logo"
      },
      // 菜单切换
      activeName: "0"
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    // 选中菜单
    handleClick(tab, event) {
      // this.searchBarFixed = false;
      // if (this.$refs["userListFrom"]) {
      //   this.$refs["userListFrom"].clearValidate();
      // }
    },
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 300
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 上传图片-成功
    handSuccess(res, file, fileList) {
      // console.log(res, file, fileList);
      this.form.site_logo = res.data.path;
      this.form.pic = res.data.src;
    },
    // 上传公章图片-成功
    handSuccess_offical_seal(res, file, fileList) {
      this.form.offical_seal = res.data.src;
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isLt2M) {
        this.$message.error("上传LOGO大小不能超过 1MB!");
      }
      return isLt2M;
    },
    // 保存
    preservation() {
      if (this.activeName == 0) {
        this.preservation_type0();
      } else if (this.activeName == 1) {
        this.preservation_type1();
      }
    },
    //基本信息保存
    preservation_type0() {
      this.$refs["userListFrom"].validate(valid => {
        if (valid) {
          let sz = JSON.parse(JSON.stringify(this.form));
          let { pic, ...form } = sz;
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
              )
            },
            apiUrl().name,
            form
          );
          setting_company(data).then(res => {
            if (res.errcode == 0) {
              this.$message({
                message: "保存成功",
                type: "success"
              });
            } else {
              this.$message.error(res.errmsg);
            }
          });
        }
      });
    },
    // 公司资质保存
    preservation_type1() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        {
          company_qualification: this.form.company_qualification
        }
      );
      setting_company(data).then(res => {
        if (res.errcode == 0) {
          this.$message({
            message: "保存成功",
            type: "success"
          });
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 基本信息
    setting_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name
      );
      setting_info(data).then(res => {
        if (res.errcode == 0) {
          if (res.data.info) {
            if (res.data.info.site_logo) {
              this.form.pic = this.avatar.domain + res.data.info.site_logo;
            }
            this.form.site_logo = res.data.info.site_logo;
            this.form.site_title = res.data.info.site_title;
            this.form.offical_seal = res.data.info.offical_seal;
            this.form.site_phone = res.data.info.site_phone;
            this.form.company_address = res.data.info.company_address;
            this.form.receive_man = res.data.info.receive_man;
            this.form.receive_mobile = res.data.info.receive_mobile;
            this.form.receive_address = res.data.info.receive_address;
            this.form.site_notice = res.data.info.site_notice;
            this.form.company_qualification =
              res.data.info.company_qualification;
            this.form.company_fullname = res.data.info.company_fullname;
            this.form.wx_customer_service = res.data.info.wx_customer_service;
            this.form.corpId = res.data.info.corpId;
          }
        } else {
          this.$message.error(res.errmsg);
        }
      });
    }
  },
  created() {
    // 基本信息
    this.setting_info();
  }
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
/deep/.el-tabs__header {
  margin: 0 0 14px;
}
.global_company_info {
  .mainua_form {
    .mainua_an {
      /deep/.el-form-item__content {
        margin-left: 0px !important;
      }
      .reset {
        background: $subActive;
        color: $menuText;
        border-color: $subActive;

        &:hover,
        &:focus {
          border-color: $menuActiveText;
          color: $menuActiveText;
        }
      }
    }
  }
  padding: 0 10px 10px 10px;
  margin: 0 15px 0 15px;
  /deep/.el-input {
    width: 400px;
  }
  /deep/.el-textarea__inner {
    width: 600px !important;
    height: 200px;
  }
  .global_cu {
    font-size: 12px;
    color: #eb0a0a;
    margin-top: -10px;
    padding-left: 7px;
  }
}
/deep/.el-tabs__item:focus.is-active.is-focus:not(:active) {
  box-shadow: none !important;
}
</style>
